#include <iostream>

#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <cstring>
#include <arpa/inet.h>

#define SERVER_PORT 9999
#define BUFFER_SIZE 128

int main()
{
    /* 创建套接字的句柄 */
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == -1)
    {
        perror("socket error:");
        exit(-1);
    }

    struct sockaddr_in serverAddr;
    /* 清空脏数据 */
    memset(&serverAddr, 0, sizeof(serverAddr));

    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(SERVER_PORT);

    const char *serverIp = "192.168.15.128";
    inet_pton(sockfd, serverIp, &(serverAddr.sin_addr.s_addr));

    connect(sockfd, (const struct sockaddr *)&serverAddr, sizeof(serverAddr));
    // const char *sendBuffer = "hello world!";

    char sendBuffer[BUFFER_SIZE] = {0};
    while (1)
    {
        printf("请输入信息:");
        scanf("%s", sendBuffer);
        sendto(sockfd, sendBuffer, strlen(sendBuffer), 0, (const struct sockaddr *)&serverAddr, sizeof(serverAddr));
    }

    return 0;
}